﻿Ext.define('Maintenance', {
    extend: 'Ext.data.Model',
    fields: [
        "Id"
	    , "Category"   //分类
	    , "Code"   //编号
	    , "Name"   //名称
	    , "Order"   //序号    
    ],
    idProperty: "Id"
});

Ext.define('MaintenClass', {
    extend: 'Ext.data.Model',
    fields: [
        "Id"
        , "Text"   //名称
        , "OldText"  //原名称
    ],
    idProperty: "Id"
});

Ext.define('SHApp.System.Maintenance.Tree', {
    extend: 'SH.Core.BaseTree',
    requires: ["SHApp.System.Maintenance.Form"],
    fields: ["id", "leaf", "text", "Category", "CId", "Code", "Order", "expanded", "isVisible", "children", "iconCls"],
    dataUrl:"api/maintenanceNode?format=json",
    rootVisible: true,
    rootText: "所有分类",
    autoSync: false,
    pvalue: 0,
    btnRefreshID: "_btnRefresh",
    initComponent: function () {
        var me = this;
        Ext.apply(this);
        me.btnAddID += me.id;
        me.btnEditID += me.id;
        me.btnDeleteID += me.id;
        me.btnRefreshID += me.id;
        this.createBar();
        this.callParent(arguments);
        this.checkBtnPermission(me.pvalue);
    },
    createBar: function () {
        var me = this;
        me.tbar = ["->",
            {
                xtype: "button",
                text: "测试",
                iconCls: 'icon-btn-add',
                handler: function () {
                    var win = me.getForm("SHApp.System.Maintenance.FormTest");
                    win.Show();
                }
            }, { xtype: "button",
                text: "添加",
                id: me.btnAddID,
                iconCls: 'icon-btn-add',
                handler: function () {
                    var selectedNode = me.getSelectionModel().getSelection(),
                        parent;
                    if (selectedNode.length <= 0) {
                        Ext.Msg.alert("提示信息", "请选择一个分类！");
                        return;
                    } else {
                        parent = selectedNode[0];
                    }
                    if (parent.data.leaf) {
                        Ext.Msg.alert("提示信息", "最多只能添加两级分类！");
                        return;
                    } else if (parent.data.root) {
                        var winClass = me.getForm("SHApp.System.Maintenance.FormClass");
                        winClass.loadRecord(new MaintenClass({ Id: null }));
                        winClass.Show("添加");
                    } else {
                        var index = parent.childNodes.length + 1,
                            category = parent.data.text;
                        if (category == "") {
                            Ext.Msg.alert("提示", "分类不能为空!");
                            return;
                        }
                        var win = me.getForm("SHApp.System.Maintenance.Form");
                        win.loadRecord(new Maintenance({ Id: null, Category: category, Order: index }));
                        win.Show("添加");
                    }
                }
            }, {
                xtype: 'button',
                text: '修改',
                id: me.btnEditID,
                iconCls: 'icon-btn-edit',
                handler: function () {
                    var selectedNode = me.getSelectionModel().getSelection()[0];
                    if (selectedNode.length <= 0) {
                        Ext.Msg.alert("提示信息", "请选择要修改的类别！");
                        return;
                    }
                    if (selectedNode.data.root) {
                        Ext.Msg.alert("提示信息", "所有分类不允许修改！");
                        return;
                    }
                    if (selectedNode.parentNode.data.text == "所有分类") {
                        var win = me.getForm("SHApp.System.Maintenance.FormClass");
                        win.submitUrl = "api/maintenclassModify?format=json";
                        selectedNode.data.Id = selectedNode.data.id;
                        selectedNode.data.Text = selectedNode.data.text;
                        selectedNode.data.OldText = selectedNode.data.text
                        me.EditData(win, selectedNode);
                    } else {
                        var f = me.getForm("SHApp.System.Maintenance.Form");
                        selectedNode.data.Id = selectedNode.data.id;
                        var text = selectedNode.data.text,
                            i = text.indexOf('(');
                        text = text.substring(0, i);
                        selectedNode.data.Name = text;
                        me.EditData(f, selectedNode);
                    }
                }
            }, { xtype: "button",
                text: "删除",
                id: me.btnDeleteID,
                iconCls: "icon-btn-delete",
                handler: function () {
                    var rs = me.getSelectionModel().getSelection();
                    if (rs.length > 0) {
                        rs = rs[0];
                        if (rs.data.root) {
                            Ext.Msg.alert("提示信息", "所有分类不允许删除！");
                            return;
                        }
                        if (rs.isExpandable() || rs.hasChildNodes()) {
                            Ext.Msg.alert("提示信息", "请先删除所有子项，再删除该类别！");
                            return;
                        }
                        if (rs.parentNode.data.text == "所有分类") {
                            me.dataUrl = "api/maintenclass?format=json";
                        } else {
                            me.dataUrl = "api/maintenanceNode?format=json";
                        }
                        var content = "确定删除：" + rs.data.text + "吗？";
                        Ext.Msg.confirm("提示信息", content, function (btn) {
                            if (btn == "yes") {
                                var rs = me.getSelectionModel().getSelection();
                                if (rs.length > 0) {
                                    Ext.Ajax.request({
                                        method: "DELETE",
                                        url: me.dataUrl + "&Id=" + rs[0].data.id,
                                        success: function () {
                                            me.store.load();
                                            me.view.select(0);
                                            me.view.focus(false);
                                        },
                                        failure: function () {
                                            Ext.Msg.alert("提示信息", "删除失败！");
                                        }
                                    });
                                    me.view.select(0);
                                    me.view.focus(false);
                                }
                            }
                        }, me);
                    }
                }
            }, "|",
		    { xtype: "button",
		        id: me.btnRefreshID,
		        iconCls: "icon-btn-refresh",
		        text: "刷新",
		        handler: function () {
		            me.store.load();
		        }
		    }
	    ]
    },
    getForm: function (form) {//获取所有表单
        var me = this;
        if (me.editForm == null || me.editForm.isDestroy) {
            var frm = Ext.create(form);
            frm.on("OnSubmit", function (action) {
                me.store.load();
            });
            me.editForm = frm;
        } else {
            if (me.editForm.$className != form) {
                var frm = Ext.create(form);
                frm.on("OnSubmit", function (action) {
                    me.store.load();
                });
                me.editForm = frm;
            }
        }
        return me.editForm;
    },
    EditData: function (win, rs) {
        if (rs != null) {
            win.loadRecord(rs);
            win.Show("编辑：" + rs.data.text);
        }
    }
});